<template>
    <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="按用户标签群发" name="tag">
            <Form
                    ref="formTag"
                    :form-items="formTagItems"
            ></Form>
        </el-tab-pane>
        <el-tab-pane label="按用户openId列表群发" name="openId">
            <Form
                    ref="formOpenId"
                    :form-items="formOpenIdItems"
            ></Form>
        </el-tab-pane>
        <el-tab-pane label="群发预览功能" name="preview">
            <el-alert type="warning" class=" g-margin-bottom15">预览功能公用于测试消息排版，查看消息内容样式</el-alert>
            <Form
                    ref="formPreview"
                    :form-items="formPreviewItems"
            ></Form>
        </el-tab-pane>
    </el-tabs>
</template>

<script>
    import Form from '../../../../../common-util/src/components/page/Form.vue'
    export default {
        components:{
            Form
        },
        name: "MassDoSend",
        props:{
            // 额外的form属性
            formData:{
                type: [Object,Function]
            },
            sendIgnoreReprint:{
                type: Boolean,
                default: false
            }
        },
        data(){
            return {
                activeName: 'tag',
                formTagItems:[
                    {
                        field: {
                            name: 'sendAll'
                        },
                        element:{
                            type: 'switch',
                            label: '是否发送给所有人',
                            required: true
                        }
                    },
                    {
                        field: {
                            name: 'tagId'
                        },
                        element:{
                            label: '标签',
                            placeholder: '从用户标签管理获取标签id，不支持多个',
                            required: true,
                            show: (form)=>{ return !form.sendAll}
                        }
                    },
                    {
                        field: {
                            name: 'sendIgnoreReprint'
                        },
                        element:{
                            type: 'switch',
                            switch:{
                              activeText: '继续群发（转载）',
                              inactiveText: '停止群发'
                            },
                            label: '图文消息被判定为转载时，是否继续群发',
                            required: true,
                            show: this.sendIgnoreReprint
                        }
                    },
                    {
                        element:{
                            type: 'button',
                            button:[
                                {
                                    action: this.sendTag,
                                    code: 'sendTagButtonCode',
                                    label: '确认群发'
                                }
                            ]
                        }
                    }
                ],
                formOpenIdItems:[
                    {
                        field: {
                            name: 'toUsers'
                        },
                        element:{
                            type: 'textarea',
                            label: 'openId',
                            required: true,
                            placeholder: '多个以逗号分隔'
                        }
                    },
                    {
                        element:{
                            type: 'button',
                            button:[
                                {
                                    action: this.sendOpenIds,
                                    code: 'sendOpenIdsButtonCode',
                                    label: '确认群发'
                                }
                            ]
                        }
                    }
                ],
                formPreviewItems:[
                    {
                        field: {
                            name: 'toWxUserName'
                        },
                        element:{
                            label: '微信号',
                            required: (form)=>{return !form.toWxUserOpenid},
                        }
                    },
                    {
                        field: {
                            name: 'toWxUserOpenid'
                        },
                        element:{
                            label: 'openId',
                            required: (form)=>{return !form.toWxUserName},
                        }
                    },
                    {
                        element:{
                            type: 'button',
                            button:[
                                {
                                    action: this.sendPreview,
                                    code: 'sendPreviewButtonCode',
                                    label: '确认发送预览消息'
                                }
                            ]
                        }
                    }
                ]
            }
        },
        methods:{
            doSend(url,form,buttonloadingCallback){
                buttonloadingCallback(true)
                this.axios.post(url,form)
                    .then(res=>{
                        this.$message.success('恭喜发送成功')
                    }).catch(error=>{
                    if(error.response){
                        this.$message.error(error.response.data.msg)
                    }else {
                        this.$message.error('网络错误或服务不可用')
                    }
                }).finally(()=>{
                    buttonloadingCallback(false)
                })
            },

            ensureFormData(){
                let form = this.formData
                if(typeof form == 'function'){
                    form = form()
                    if(form === false){
                        return false
                    }
                }
                if(!form || Object.keys(form).length == 0){
                    this.$message.error('参数不正确')
                    return false
                }
                return form
            },
            sendTag(){
                let form = this.ensureFormData()
                if(!form){
                    return
                }
                this.$refs.formTag.validateAndSubmitForm((formTag)=>{
                    let url = this.$baseURL.wx + '/wx/mp/mass/' + form.appCode + '/sendTag'
                    let tempForm = {}
                    for (let key in form) {
                        tempForm[key] = form[key]
                    }
                    for (let key in formTag) {
                        tempForm[key] = formTag[key]
                    }
                    this.doSend(url,tempForm,(value)=>{this.$refs.formTag.setButtonLoading('sendTagButtonCode',value)})
                })
            },
            sendOpenIds(){
                let form = this.ensureFormData()
                if(!form){
                    return
                }
                this.$refs.formOpenId.validateAndSubmitForm((formOpenId)=>{
                    let url = this.$baseURL.wx + '/wx/mp/mass/' + form.appCode + '/sendOpenIds'
                    let tempForm = {}
                    for (let key in form) {
                        tempForm[key] = form[key]
                    }
                    for (let key in formOpenId) {
                        if(key == 'toUsers'){
                            tempForm[key] = formOpenId[key].split(',')
                        }else{
                            tempForm[key] = formOpenId[key]
                        }
                    }
                    this.doSend(url,tempForm,(value)=>{this.$refs.formOpenId.setButtonLoading('sendOpenIdsButtonCode',value)})
                })
            },
            sendPreview(){
                let form = this.ensureFormData()
                if(!form){
                    return
                }
                this.$refs.formPreview.validateAndSubmitForm((formPreview)=>{
                    let url = this.$baseURL.wx + '/wx/mp/mass/' + form.appCode + '/preview'
                    let tempForm = {}
                    for (let key in form) {
                        tempForm[key] = form[key]
                    }
                    for (let key in formPreview) {
                        tempForm[key] = formPreview[key]
                    }
                    this.doSend(url,tempForm,(value)=>{this.$refs.formPreview.setButtonLoading('sendPreviewButtonCode',value)})
                })
            }
        }
    }
</script>

<style scoped>

</style>